//
//  MSCollectHeadView.m
//  MSJD
//
//  Created by 箭军刘 on 2018/11/4.
//  Copyright © 2018 xiangbo. All rights reserved.
//

#import "MSCollectHeadView.h"

@interface MSCollectHeadView()

@property (nonatomic, strong) UIButton *hotelButton; // 酒店按钮
@property (nonatomic, strong) UIButton *storeButton; // 店铺按钮
@property (nonatomic, strong) UIView *lineView; // 线控件

@end

@implementation MSCollectHeadView

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        
        self.backgroundColor = [UIColor whiteColor];
        [self initSubViews];
    }
    return self;
}

#pragma mark - Init Method

- (void)initSubViews {
    
    CGFloat buttonWidth = MS_SCREEN_WIDTH / 2.0;
    [self addSubview:self.hotelButton];
    [self.hotelButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.bottom.offset(0);
        make.width.offset(buttonWidth);
    }];
    [self addSubview:self.storeButton];
    [self.storeButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.right.bottom.offset(0);
        make.width.offset(buttonWidth);
    }];
    
    // 添加lineView
    CGFloat lineHeight = 2.0;
    CGFloat lineWidth = 46.0;
    [self addSubview:self.lineView];
    [self.lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.offset(0);
        make.width.offset(lineWidth);
        make.height.offset(lineHeight);
        make.centerX.equalTo(self.hotelButton.mas_centerX);
    }];
}

#pragma mark - Public Method

- (void)changeHeadViewSelectButton:(NSInteger)index {
    if (index == 0) {
        // 修改的是酒店按钮
        if (self.hotelButton.selected) {
            return;
        }
        [self hotelButtonClick:self.hotelButton];
    } else {
        if (self.storeButton.selected) {
            return;
        }
        [self storeButtonClick:self.storeButton];
    }
}

#pragma mark - Event Response
/** 酒店按钮点击 */
- (void)hotelButtonClick:(UIButton *)sender {
    if (sender.selected) {
        return;
    }
    // 修改按钮的选中状态
    sender.selected = YES;
    self.storeButton.selected = NO;
    
    CGPoint center = self.lineView.center;
    CGPoint hotelCenter = self.hotelButton.center;
    [UIView animateWithDuration:0.25 animations:^{
        self.lineView.center = CGPointMake(hotelCenter.x, center.y);
    }];
    if (self.HotelButtonClickBlock) {
        self.HotelButtonClickBlock(0);
    }
}
/** 店铺按钮点击 */
- (void)storeButtonClick:(UIButton *)sender {
    if (sender.selected) {
        return;
    }
    // 修改按钮的选中状态
    sender.selected = YES;
    self.hotelButton.selected = NO;
    
    CGPoint center = self.lineView.center;
    CGPoint storeCenter = self.storeButton.center;
    [UIView animateWithDuration:0.25 animations:^{
        self.lineView.center = CGPointMake(storeCenter.x, center.y);
    }];
    if (self.HotelButtonClickBlock) {
        self.HotelButtonClickBlock(1);
    }
}

#pragma mark - Getters

- (UIButton *)hotelButton {
    if (!_hotelButton) {
        _hotelButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_hotelButton setTitle:@"酒店" forState:UIControlStateNormal];
        [_hotelButton setTitle:@"酒店" forState:UIControlStateSelected];
        [_hotelButton setTitleColor:[UIColor colorWithHexString:@"#444444"] forState:UIControlStateNormal];
        [_hotelButton setTitleColor:MSThemeColor forState:UIControlStateSelected];
        [_hotelButton addTarget:self action:@selector(hotelButtonClick:) forControlEvents:UIControlEventTouchUpInside];
        _hotelButton.titleLabel.font = [UIFont systemFontOfSize:14];
        _hotelButton.selected = YES;
    }
    return _hotelButton;
}

- (UIButton *)storeButton {
    if (!_storeButton) {
        _storeButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_storeButton setTitle:@"店铺" forState:UIControlStateNormal];
        [_storeButton setTitle:@"店铺" forState:UIControlStateSelected];
        [_storeButton setTitleColor:[UIColor colorWithHexString:@"#444444"] forState:UIControlStateNormal];
        [_storeButton setTitleColor:MSThemeColor forState:UIControlStateSelected];
        [_storeButton addTarget:self action:@selector(storeButtonClick:) forControlEvents:UIControlEventTouchUpInside];
        _storeButton.titleLabel.font = [UIFont systemFontOfSize:14];
    }
    return _storeButton;
}

- (UIView *)lineView {
    if (!_lineView) {
        _lineView = [[UIView alloc] init];
        _lineView.backgroundColor = MSThemeColor;
    }
    return _lineView;
}

@end
